package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.form.SaveRoleAccountForm;
import com.woniuxy.controller.info.Result;
import com.woniuxy.model.AccountRule;
import com.woniuxy.service.AccountRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-16
 */
@RestController
@RequestMapping("/accountRule")
public class AccountRuleController {
    @Autowired
    private AccountRuleService accountRuleService;
    @GetMapping("getRoleIdByAccountId/{id}")
    public Result getRoleIdByAccountId(@PathVariable("id") Integer accountId){
        QueryWrapper<AccountRule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",accountId);
        List<AccountRule> list = accountRuleService.list(queryWrapper);
        List<Integer> collect = list.stream().map(rm -> rm.getRuleId()).collect(Collectors.toList());
        return Result.getInstanceList(Integer.class).setData(collect);
    }

    @PostMapping("saveRoleByAccountId")
    public Result saveRoleByAccountId(@RequestBody SaveRoleAccountForm saveRoleAccountForm){
       return accountRuleService.saveRoleByAccountId(saveRoleAccountForm);
    }


}

